﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Actuals.CoreDomain.Business;
using Actuals.Business;
using Actuals.CoreDomain;

namespace Actuals
{
    public partial class AddTasks : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["EmployeeId"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            Response.AddHeader("Cache-control", "no-store, must-revalidate,private,no-cache");
            Response.AddHeader("Pragma", "no-cache");
            Response.AddHeader("Expires", "0");

            Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            if (!Page.IsPostBack)
            {
                IBusinessProject businessProject = new BusinessProject();
                List<IProject> Projects = businessProject.GetProjects();
                ddlProject.DataSource = Projects;
                ddlProject.DataTextField = "ProjectDescription";
                ddlProject.DataValueField = "ProjectId";
                ddlProject.DataBind();
                ddlProject.Items.Insert(0, "Select");


                IBusinessTaskStatus businessTaskStatus = new BusinessTaskStatus();
                IList<ITaskStatus> status = businessTaskStatus.GetTaskStatuses();
                ddlStatus.DataSource = status;
                ddlStatus.DataTextField = "StatusDescription";
                ddlStatus.DataValueField = "StatusId";
                ddlStatus.DataBind();
                ddlStatus.Items.Insert(0, "Select");




                if (Request.QueryString["Mode"] != null)
                {

                    string _mode = Request.QueryString["Mode"].ToString();
                    ViewState["Mode"] = _mode;
                    string _taskId = Request.QueryString["TaskId"].ToString();
                    hidTaskId.Value = _taskId;
                    PopulateTasks(int.Parse(_taskId));
                }
            }
        }




        protected void PopulateTasks(int taskId)
        {

            IBusinessTask businessTask = new BusinessTask();
            ITask task = businessTask.GetTask(taskId);

            ddlProject.SelectedValue = task.ProjectId.ToString();
            FillRelease(task.ProjectId.ToString());
            ddlRelease.SelectedValue = task.ReleaseId.ToString();
            txtDescription.Text = task.Description.ToString();
            txtTaskShortName.Text = task.TaskShortName.ToString();
            ddlStatus.SelectedValue = task.Status.ToString();
            txtInitialEstimation.Text = task.InitialEstimation.ToString();
            txtCurrentEstimation.Text = task.CurrentEstimation.ToString();
            ddlTaskType.SelectedValue = task.TaskType;
            ddlPriority.SelectedValue = task.Priority.ToString();

        }





        protected void FillRelease(string projetcId)
        {
            IBusinessRelease businessRelease = new BusinessRelease();
            IList<IRelease> release = businessRelease.GetReleases(int.Parse(projetcId));
            ddlRelease.DataSource = release;
            ddlRelease.DataTextField = "Description";
            ddlRelease.DataValueField = "ReleaseId";
            ddlRelease.DataBind();
            ListItem select = new ListItem();
            select.Value = "0";
            select.Text = "Select";
            ddlRelease.Items.Insert(0, select);

        }

        protected void ddlProject_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            FillRelease(ddlProject.SelectedValue);

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                ITask task = new Task();
                task.ProjectId = int.Parse(ddlProject.SelectedValue);
                task.ReleaseId = int.Parse(ddlRelease.SelectedValue);

                string _taskType = string.Empty;

                switch (ddlTaskType.SelectedValue)
                {
                    case "tech":
                        _taskType = "tech";
                        break;
                    case "func":
                        _taskType = "func";
                        break;
                    case "other":
                        _taskType = "other";
                        break;
                }

                task.TaskType = _taskType;
                task.TaskShortName = txtTaskShortName.Text;
                task.Description = txtDescription.Text;
                task.InitialEstimation = int.Parse(txtInitialEstimation.Text);
                task.CurrentEstimation = int.Parse(txtCurrentEstimation.Text);
                task.Status = int.Parse(ddlStatus.SelectedValue);
                task.Priority = int.Parse(ddlPriority.SelectedValue);
                IBusinessTask businessTask = new BusinessTask();
                if (ViewState["Mode"] != null)
                {
                    task.TaskId = Int32.Parse(hidTaskId.Value);
                    businessTask.UpdateTask(task);

                }
                else
                    businessTask.AddTask(task);
                lblMsg.Text = "Saved";


            }
            catch (Exception ex)
            {

                lblMsg.Text = ex.Message;
            }

        }
















    }
}










